const path = require('path')
const HtmlWebPackPlugin = require('mini-css-extract-plugin')
const MiniCssExtractPlugin = require('html-webpack-plugin')
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
    entry: './src/app.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'main.js',
    },
    mode: 'production',
    module: {
        rules: [
            // 处理 css
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader'],
            },
            // 处理 less
            {
                test: /\.less$/i,
                use: ['style-loader', 'css-loader', 'less-loader'],
            },
            // 处理 sass
            {
                test: /\.s[ac]ss$/i,
                use: ['style-loader', 'css-loader', 'sass-loader'],
            },
            // 处理 图片
            {
                test: /\.(png|jpe?g|gif|svg|webp)$/,
                type: 'asset',
                parser: {
                    dataUrlCondition: {
                        maxSize: 200 * 1024
                    }
                }
            },
            // {
            //     test: /\.css$/,
            //     use: {
            //         MiniCssExtractPlugin.loader,
            //         'css-loader',
            //         {
            //             loader: 'postcss-loader',
            //             options :{
            //                 postcssOptions: {
            //                     plugins: ['postcss-preset-env']
            //                 }
            //             }
            //         }
            //     }
            // }
        ],
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'static/css/app.css'
        }),
        // new HtmlWebPackPlugin({
        //     template: path.resolve(__dirname, 'index.html')
        // }),
        new CssMinimizerPlugin(),
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        new CleanWebpackPlugin()
    ],

    devServer: {
        host: 'localhost',
        open: true,
        port: 8080
    }
}